﻿// ********************************
// 2010.10.29
// 主机信息
// Author:卢融凯
// ********************************
using System;
using System.Collections.Generic;
using System.Linq;

namespace SManage.Model.Trace
{
    public class HostInfo
    {
        public IList<StorageItem> StorageInfo { get; set; }
        public IList<ProcessorDescription> ProcessorInfo { get; set; }

        public HostInfo() { }
        public HostInfo(IList<StorageItem> storageInfo, IList<ProcessorDescription> processorInfo)
        {
            StorageInfo = storageInfo;
            ProcessorInfo = processorInfo;
        }

        public EasyHost ToEasyHost() {
            if (StorageInfo == null || ProcessorInfo == null){
                return null;
            }

            var easyHost = new EasyHost { 
                CpuCount = ProcessorInfo.Count() 
            };

            var cpuLoad = new List<int>();
            foreach (var pi in ProcessorInfo) {
                cpuLoad.Add(pi.hrProcessorLoad);
            }
            easyHost.CpuLoad = cpuLoad;

            var memory = StorageInfo.Where(si=>si.hrStorageType=="hrStorageRam").First();
            easyHost.MemoryTotal = (Double)memory.hrStorageAllocationUnits * (Double)memory.hrStorageSize;
            easyHost.MemoryUsed = (Double)memory.hrStorageAllocationUnits * (Double)memory.hrStorageUsed;

            var hardDisk = new List<StorageDescription>();
            foreach (var hd in StorageInfo.Where(si => si.hrStorageType == "hrStorageFixedDisk")) {
                var simplyStorage = new StorageDescription() { 
                    Description = hd.hrStorageDescr,
                    PartitionTotal = (Double)hd.hrStorageAllocationUnits * (Double)hd.hrStorageSize,
                    PartitionUsed = (Double)hd.hrStorageAllocationUnits * (Double)hd.hrStorageUsed
                };
                hardDisk.Add(simplyStorage);
            }
            easyHost.HardDisk = hardDisk;
            return easyHost;
        }
    }
}
